<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label=" 0未接单，1已接单，3已完成" prop="state">
      <el-input v-model="dataForm.state" placeholder=" 0未接单，1已接单，3已完成"></el-input>
    </el-form-item>
    <el-form-item label="订单标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="订单标题"></el-input>
    </el-form-item>
    <el-form-item label="订单内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="订单内容"></el-input>
    </el-form-item>
    <el-form-item label="订单价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="订单价格"></el-input>
    </el-form-item>
    <el-form-item label="订单图片" prop="orderImg">
      <el-input v-model="dataForm.orderImg" placeholder="订单图片"></el-input>
    </el-form-item>
    <el-form-item label="订单备注" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="订单备注"></el-input>
    </el-form-item>
    <el-form-item label="联系人名称，接单才可看" prop="contacts">
      <el-input v-model="dataForm.contacts" placeholder="联系人名称，接单才可看"></el-input>
    </el-form-item>
    <el-form-item label="联系人电话，接单才可看" prop="contactsPhone">
      <el-input v-model="dataForm.contactsPhone" placeholder="联系人电话，接单才可看"></el-input>
    </el-form-item>
    <el-form-item label="从哪送" prop="contactsAddressFrom">
      <el-input v-model="dataForm.contactsAddressFrom" placeholder="从哪送"></el-input>
    </el-form-item>
    <el-form-item label="送到哪" prop="contactsAddressTo">
      <el-input v-model="dataForm.contactsAddressTo" placeholder="送到哪"></el-input>
    </el-form-item>
    <el-form-item label="发布人id" prop="issueUser">
      <el-input v-model="dataForm.issueUser" placeholder="发布人id"></el-input>
    </el-form-item>
    <el-form-item label="是否匿名0不匿名，1匿名" prop="hideName">
      <el-input v-model="dataForm.hideName" placeholder="是否匿名0不匿名，1匿名"></el-input>
    </el-form-item>
    <el-form-item label="添加时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="添加时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          state: '',
          title: '',
          content: '',
          price: '',
          orderImg: '',
          remarks: '',
          contacts: '',
          contactsPhone: '',
          contactsAddressFrom: '',
          contactsAddressTo: '',
          issueUser: '',
          hideName: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          state: [
            { required: true, message: ' 0未接单，1已接单，3已完成不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '订单标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '订单内容不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '订单价格不能为空', trigger: 'blur' }
          ],
          orderImg: [
            { required: true, message: '订单图片不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '订单备注不能为空', trigger: 'blur' }
          ],
          contacts: [
            { required: true, message: '联系人名称，接单才可看不能为空', trigger: 'blur' }
          ],
          contactsPhone: [
            { required: true, message: '联系人电话，接单才可看不能为空', trigger: 'blur' }
          ],
          contactsAddressFrom: [
            { required: true, message: '从哪送不能为空', trigger: 'blur' }
          ],
          contactsAddressTo: [
            { required: true, message: '送到哪不能为空', trigger: 'blur' }
          ],
          issueUser: [
            { required: true, message: '发布人id不能为空', trigger: 'blur' }
          ],
          hideName: [
            { required: true, message: '是否匿名0不匿名，1匿名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/work/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.state = data.work.state
                this.dataForm.title = data.work.title
                this.dataForm.content = data.work.content
                this.dataForm.price = data.work.price
                this.dataForm.orderImg = data.work.orderImg
                this.dataForm.remarks = data.work.remarks
                this.dataForm.contacts = data.work.contacts
                this.dataForm.contactsPhone = data.work.contactsPhone
                this.dataForm.contactsAddressFrom = data.work.contactsAddressFrom
                this.dataForm.contactsAddressTo = data.work.contactsAddressTo
                this.dataForm.issueUser = data.work.issueUser
                this.dataForm.hideName = data.work.hideName
                this.dataForm.createTime = data.work.createTime
                this.dataForm.updateTime = data.work.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/work/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'state': this.dataForm.state,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'price': this.dataForm.price,
                'orderImg': this.dataForm.orderImg,
                'remarks': this.dataForm.remarks,
                'contacts': this.dataForm.contacts,
                'contactsPhone': this.dataForm.contactsPhone,
                'contactsAddressFrom': this.dataForm.contactsAddressFrom,
                'contactsAddressTo': this.dataForm.contactsAddressTo,
                'issueUser': this.dataForm.issueUser,
                'hideName': this.dataForm.hideName,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
